package com.ti.andon.persistent.dao;

import com.ti.andon.persistent.AdPerson;
import com.ti.andon.workflow.dao.base.BaseHibernateDAO;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 	* A data access object (DAO) providing persistence and search support for AdPerson entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.ti.andon.persistent.AdPerson
  * @author MyEclipse Persistence Tools 
 */

public class AdPersonDAO extends BaseHibernateDAO  {
    private static final Log log = LogFactory.getLog(AdPersonDAO.class);
	//property constants
	public static final String FIRST_NAME = "firstName";
	public static final String ADDRESS = "address";
	public static final String POSTAL_CODE = "postalCode";
	public static final String PRIMARY_EMAIL = "primaryEmail";
	public static final String SECONDARY_EMAIL = "secondaryEmail";
	public static final String PHONE_NUMBER = "phoneNumber";
	public static final String MOBILE_NUMBER = "mobileNumber";
	public static final String SECOND_NAME = "secondName";
	public static final String LAST_NAME = "lastName";
	public static final String EMPLOYEE_CODE = "employeeCode";
	public static final String CHANGE_BY = "changeBy";



    
    public void save(AdPerson transientInstance) {
        log.debug("saving AdPerson instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(AdPerson persistentInstance) {
        log.debug("deleting AdPerson instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public AdPerson findById( java.lang.Long id) {
        log.debug("getting AdPerson instance with id: " + id);
        try {
            AdPerson instance = (AdPerson) getSession()
                    .get("com.ti.andon.persistent.AdPerson", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(AdPerson instance) {
        log.debug("finding AdPerson instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.ti.andon.persistent.AdPerson")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding AdPerson instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from AdPerson as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByFirstName(Object firstName
	) {
		return findByProperty(FIRST_NAME, firstName
		);
	}
	
	public List findByAddress(Object address
	) {
		return findByProperty(ADDRESS, address
		);
	}
	
	public List findByPostalCode(Object postalCode
	) {
		return findByProperty(POSTAL_CODE, postalCode
		);
	}
	
	public List findByPrimaryEmail(Object primaryEmail
	) {
		return findByProperty(PRIMARY_EMAIL, primaryEmail
		);
	}
	
	public List findBySecondaryEmail(Object secondaryEmail
	) {
		return findByProperty(SECONDARY_EMAIL, secondaryEmail
		);
	}
	
	public List findByPhoneNumber(Object phoneNumber
	) {
		return findByProperty(PHONE_NUMBER, phoneNumber
		);
	}
	
	public List findByMobileNumber(Object mobileNumber
	) {
		return findByProperty(MOBILE_NUMBER, mobileNumber
		);
	}
	
	public List findBySecondName(Object secondName
	) {
		return findByProperty(SECOND_NAME, secondName
		);
	}
	
	public List findByLastName(Object lastName
	) {
		return findByProperty(LAST_NAME, lastName
		);
	}
	
	public List findByEmployeeCode(Object employeeCode
	) {
		return findByProperty(EMPLOYEE_CODE, employeeCode
		);
	}
	
	public List findByChangeBy(Object changeBy
	) {
		return findByProperty(CHANGE_BY, changeBy
		);
	}
	

	public List findAll() {
		log.debug("finding all AdPerson instances");
		try {
			String queryString = "from AdPerson";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public AdPerson merge(AdPerson detachedInstance) {
        log.debug("merging AdPerson instance");
        try {
            AdPerson result = (AdPerson) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(AdPerson instance) {
        log.debug("attaching dirty AdPerson instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(AdPerson instance) {
        log.debug("attaching clean AdPerson instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}